require(['jgGrid', 'bootstrap', 'underscore', 'cookie'], function () {
    var webRoot = G_WEB_ROOT;
    var grid_selector = "#productGrid";
    var pager_selector = "#productGridPager";

    var urls = {
        findOnlineOpt: webRoot + "/sys/findOnlineOpt",
        export: webRoot + "/sys/export",
        deleteOnlineOptByIds : webRoot + '/sys/deleteOnlineOptByIds'
    };

    var selectClassifyId = '';

    var gridObj = {
        searchTxt: null,
        gridId: grid_selector,
        gridOpt: {
            url: urls.findOnlineOpt,
            datatype: 'json',
            mtype: 'POST',
            autowidth: true,
            height: 630,
            colNames: ['id', '名称', '手机', '身份证号', '是否办理过分期', '描述', '创建时间'],
            colModel: [
                {name: 'id', hidedlg: true, hidden: true, width: 60},
                { name: 'userName', sortable: false, editable: true, width: 50, align: 'center' },
                { name: 'userPhoneNum', sortable: false, editable: true, width: 100, align: 'center' },
                { name: 'userIDCard', sortable: false, editable: true, width: 100, align: 'center' },
                { name: 'userStages', sortable: false, editable: true, width: 150, align: 'center' },
                { name: 'userNote', width: 200, sortable: false, editable: true, align: 'center', editoptions: { size: "20", maxlength: "5" }, editrules: { required: true } },
                {
                    name: 'createDate', width: 100, sortable: false, align: 'center', editable: false, sorttype: "date", editrules: { required: true },
                    formatter: function (v, o, r) {
                        return getSmpFormatDateByLong(r.createDate, false);
                    }
                },
                //		            {name:'myac',width:200,fixed:true,sortable:false,align:'center',resize:false,search:false,title:false}
            ],
            viewrecords: true,
            pager: pager_selector,
            altRows: true,
            rowHeight: 36,
            styleUI: 'Bootstrap',
            multiselect: true,
            multiboxonly: true,
            onSelectRow: function (id) {
            }
        },
        init: function (_searchTxt) {
            var self = this;
            $(self.gridId).jqGrid(self.gridOpt);
        },
        initGrid: function (_searchTxt) {
            var self = this;
            $(self.gridId).clearGridData().
                setGridParam({ postData: { 'searchTxt': _searchTxt } }).trigger("reloadGrid");
            self.resizeGrid();
        },
        resizeGrid: function () {
            var self = this;
            var parentDom = $('#gbox_' + self.gridId.split('#')[1]).parent();
            $(self.gridId).setGridWidth(parentDom.width());
        }
    };



    // EVENT
    //=====================================================
    $("#searchBtnId").click(function () {
        var searchTxt = $("#searchNameTxt").val();
        gridObj.initGrid(searchTxt);
    });

    $("#delBtnId").unbind().bind('click',function(){
		var ids = $(gridObj.gridId).jqGrid('getGridParam','selarrrow');
		console.log(ids);
		if(ids.length > 0){
            $.post(urls.deleteOnlineOptByIds, {'idsArr':ids.join()},function(rs){
            	if(rs.type){
            		alert("删除成功");
            		gridObj.initGrid(null);
            	}else{
            		alert("删除失败");
            	}
              });
        }else{
            alert("请选择需要删除的记录！");
        }
	});

    $("#exportBtnId").click(function () {
        var self = this;
        var form = $("<form>");//定义一个form表单
        form.attr("style", "display:none");
        form.attr("target", "");
        form.attr("method", "post");
        form.attr("action", urls.export);
        var input1 = $("<input>");
        input1.attr("type", "hidden");
        // input1.attr("name", "yearmonth");
        // input1.attr("value", "123");
        $("body").append(form);//将表单放置在web中
        form.append(input1);
        form.submit();//表单提交
        // self.setExportShowFun();
        // setTimeout(
        //     function () {
        //         $('#' + self.exportId).modal('hide');
        //     }, 3000);
        // setTimeout(
        //     function () {
        //         $('#' + self.modalId).modal('hide');
        //     }, 6000);
    });
    //=====================================================

    $(function () {
        gridObj.init();
    });



    //扩展Date的format方法
    Date.prototype.format = function (format) {
        var o = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S": this.getMilliseconds()
        }
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    }
    /**
    *转换日期对象为日期字符串
    * @param date 日期对象
    * @param isFull 是否为完整的日期数据,
    *               为true时, 格式如"2000-03-05 01:05:04"
    *               为false时, 格式如 "2000-03-05"
    * @return 符合要求的日期字符串
    */
    function getSmpFormatDate(date, isFull) {
        var pattern = "";
        if (isFull == true || isFull == undefined) {
            pattern = "yyyy-MM-dd hh:mm:ss";
        } else {
            pattern = "yyyy-MM-dd";
        }
        return getFormatDate(date, pattern);
    }
    /**
    *转换当前日期对象为日期字符串
    * @param date 日期对象
    * @param isFull 是否为完整的日期数据,
    *               为true时, 格式如"2000-03-05 01:05:04"
    *               为false时, 格式如 "2000-03-05"
    * @return 符合要求的日期字符串
    */

    function getSmpFormatNowDate(isFull) {
        return getSmpFormatDate(new Date(), isFull);
    }
    /**
    *转换long值为日期字符串
    * @param l long值
    * @param isFull 是否为完整的日期数据,
    *               为true时, 格式如"2000-03-05 01:05:04"
    *               为false时, 格式如 "2000-03-05"
    * @return 符合要求的日期字符串
    */

    function getSmpFormatDateByLong(l, isFull) {
        return getSmpFormatDate(new Date(l), isFull);
    }
    /**
    *转换long值为日期字符串
    * @param l long值
    * @param pattern 格式字符串,例如：yyyy-MM-dd hh:mm:ss
    * @return 符合要求的日期字符串
    */

    function getFormatDateByLong(l, pattern) {
        return getFormatDate(new Date(l), pattern);
    }
    /**
    *转换日期对象为日期字符串
    * @param l long值
    * @param pattern 格式字符串,例如：yyyy-MM-dd hh:mm:ss
    * @return 符合要求的日期字符串
    */
    function getFormatDate(date, pattern) {
        if (date == undefined) {
            date = new Date();
        }
        if (pattern == undefined) {
            pattern = "yyyy-MM-dd hh:mm:ss";
        }
        return date.format(pattern);
    }

});
